package com.woniuxy.microretailserver.service.impl;

import com.woniuxy.microretailserver.bean.SummaryCountBean;
import com.woniuxy.microretailserver.bean.SummaryCountDateBean;
import com.woniuxy.microretailserver.mapper.SummaryCountMapper;
import com.woniuxy.microretailserver.service.ISummaryCount;
import com.woniuxy.microretailserver.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Transactional(readOnly = true)
public class SummaryCountImpl implements ISummaryCount {
    @Resource
    private SummaryCountMapper summaryCountMapper;

    @Override
    public List<SummaryCountDateBean> findAll() {
        //Date date = DateUtil.str2Date(paymentTime, "yyyy-MM-dd");
        List<SummaryCountBean> summaryByTime = summaryCountMapper.findSummaryByTime();
       // System.out.println(summaryByTime);
        ArrayList<SummaryCountDateBean> summaryCountDateBeans = new ArrayList<>();
        SummaryCountDateBean summaryCountDateBean = new SummaryCountDateBean();
        for (int i=0;i<summaryByTime.size();i++){

            if (i!=0) {
                if (summaryByTime.get(i).getPaymentTime().equals(summaryByTime.get(i - 1).getPaymentTime())) {
                        if (summaryByTime.get(i).getPayment().equals("微信")){
                            summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                            summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                        }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                            summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                            summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                        }else if (summaryByTime.get(i).getPayment().equals("现金")){
                            summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                            summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                        }
                }else{
                    summaryCountDateBean.setTime(summaryByTime.get(i-1).getPaymentTime());
                    summaryCountDateBeans.add(summaryCountDateBean);
                    summaryCountDateBean=new SummaryCountDateBean();
                    if (summaryByTime.get(i).getPayment().equals("微信")){
                        summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                        summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("现金")){
                        summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                    }
                }
            }else {
                if (summaryByTime.get(i).getPayment().equals("微信")){
                    summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                    summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                }else if (summaryByTime.get(i).getPayment().equals("现金")){
                    summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                }
            }
        }
        summaryCountDateBean.setTime(summaryByTime.get(summaryByTime.size()-1).getPaymentTime());
        if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("微信")){
            summaryCountDateBean.setCountB(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }else if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("支付宝")){
            summaryCountDateBean.setCountA(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }else if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("现金")){
            summaryCountDateBean.setCountC(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }
        summaryCountDateBeans.add(summaryCountDateBean);
        return summaryCountDateBeans;
    }

    @Override
    public List<SummaryCountDateBean> findSummaryByTimer(String paymentTime1) {
        //Date date = DateUtil.str2Date(paymentTime, "yyyy-MM-dd");
        List<SummaryCountBean> summaryByTime = summaryCountMapper.findSummaryByTimer(paymentTime1);
        // System.out.println(summaryByTime);
        ArrayList<SummaryCountDateBean> summaryCountDateBeans = new ArrayList<>();
        SummaryCountDateBean summaryCountDateBean = new SummaryCountDateBean();
        for (int i=0;i<summaryByTime.size();i++){

            if (i!=0) {
                if (summaryByTime.get(i).getPaymentTime().equals(summaryByTime.get(i - 1).getPaymentTime())) {
                    if (summaryByTime.get(i).getPayment().equals("微信")){
                        summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                        summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("现金")){
                        summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                    }
                }else{
                    summaryCountDateBean.setTime(summaryByTime.get(i-1).getPaymentTime());
                    summaryCountDateBeans.add(summaryCountDateBean);
                    summaryCountDateBean=new SummaryCountDateBean();
                    if (summaryByTime.get(i).getPayment().equals("微信")){
                        summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                        summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                    }else if (summaryByTime.get(i).getPayment().equals("现金")){
                        summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                        summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                    }
                }
            }else {
                if (summaryByTime.get(i).getPayment().equals("微信")){
                    summaryCountDateBean.setCountB(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(i).getPaymentAmountSum());
                }else if (summaryByTime.get(i).getPayment().equals("支付宝")){
                    summaryCountDateBean.setCountA(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(i).getPaymentAmountSum());
                }else if (summaryByTime.get(i).getPayment().equals("现金")){
                    summaryCountDateBean.setCountC(summaryByTime.get(i).getCount());
                    summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(i).getPaymentAmountSum());
                }
            }
        }
        summaryCountDateBean.setTime(summaryByTime.get(summaryByTime.size()-1).getPaymentTime());
        if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("微信")){
            summaryCountDateBean.setCountB(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumB(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }else if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("支付宝")){
            summaryCountDateBean.setCountA(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumA(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }else if (summaryByTime.get(summaryByTime.size()-1).getPayment().equals("现金")){
            summaryCountDateBean.setCountC(summaryByTime.get(summaryByTime.size()-1).getCount());
            summaryCountDateBean.setPaymentAmountSumC(summaryByTime.get(summaryByTime.size()-1).getPaymentAmountSum());
        }
        summaryCountDateBeans.add(summaryCountDateBean);
        return summaryCountDateBeans;
    }
}
